home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ExampleFileFilter.java < prev    next >
Text File  |  1998-09-28  |  8KB  |  255 lines

  1. /*
  2.  * @(#)ExampleFileFilter.java    1.4 98/04/14
  3.  * 
  4.  * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21.  
  22. import java.io.File;
  23. import java.util.Hashtable;
  24. import java.util.Enumeration;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.preview.filechooser.*;
  27. import com.sun.java.swing.preview.*;
  28.  
  29. /**
  30.  * A convenience implementation of FileFilter that filters out
  31.  * all files except for those type extensions that it knows about.
  32.  * 
  33.  * Extensions are of the type ".foo", which is typically found on
  34.  * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
  35.  * 
  36.  * Example - create a new filter that filerts out all files
  37.  * but gif and jpg image files:
  38.  *
  39.  *     JFileChooser chooser = new JFileChooser();
  40.  *     ExampleFileFilter filter = new ExampleFileFilter(
  41.  *                   new String{"gif", "jpg"}, "JPEG & GIF Images")
  42.  *     chooser.addChoosableFileFilter(filter);
  43.  *     chooser.showOpenDialog(this);
  44.  
  45.  */
  46. public class ExampleFileFilter extends FileFilter {
  47.     
  48.     private static String TYPE_UNKNOWN = "Type Unknown";
  49.     private static String HIDDEN_FILE = "Hidden File";
  50.  
  51.     private Hashtable filters = null;
  52.     private String description = null;
  53.     private String fullDescription = null;
  54.     private boolean useExtensionsInDescription = true;
  55.     
  56.     /**
  57.      * Creates a file filter. If no filters are added, then all
  58.      * files are accepted.
  59.      *
  60.      * @see #addExtension
  61.      */
  62.     public ExampleFileFilter() {
  63.     this((String) null, (String) null);
  64.     }
  65.  
  66.     /**
  67.      * Creates a file filter that accepts files with the given extension.
  68.      * Example: new ExampleFileFilter("jpg");
  69.      *
  70.      * @see #addExtension
  71.      */
  72.     public ExampleFileFilter(String extension) {
  73.     this(extension,null);
  74.     }
  75.  
  76.     /**
  77.      * Creates a file filter that accepts the given file type.
  78.      * Example: new ExampleFileFilter("jpg", "JPEG Image Images");
  79.      *
  80.      * Note that the "." before the extension is not needed. If
  81.      * provided, it will be ignored.
  82.      *
  83.      * @see #addExtension
  84.      */
  85.     public ExampleFileFilter(String extension, String description) {
  86.     this(new String[] {extension}, description);
  87.     }
  88.  
  89.     /**
  90.      * Creates a file filter from the given string array.
  91.      * Example: new ExampleFileFilter(String {"gif", "jpg"});
  92.      *
  93.      * Note that the "." before the extension is not needed adn
  94.      * will be ignored.
  95.      *
  96.      * @see #addExtension
  97.      */
  98.     public ExampleFileFilter(String[] filters) {
  99.     this(filters, null);
  100.     }
  101.  
  102.     /**
  103.      * Creates a file filter from the given string array and description.
  104.      * Example: new ExampleFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
  105.      *
  106.      * Note that the "." before the extension is not needed and will be ignored.
  107.      *
  108.      * @see #addExtension
  109.      */
  110.     public ExampleFileFilter(String[] filters, String description) {
  111.     this.filters = new Hashtable(filters.length);
  112.     for (int i = 0; i < filters.length; i++) {
  113.         // add filters one by one
  114.         addExtension(filters[i]);
  115.     }
  116.     setDescription(description);
  117.     }
  118.  
  119.     /**
  120.      * Return true if this file should be shown in the directory pane,
  121.      * false if it shouldn't.
  122.      *
  123.      * Files that begin with "." are ignored.
  124.      *
  125.      * @see #getExtension
  126.      * @see FileFilter#accepts
  127.      */ 
  128.     public boolean accept(File f) {
  129.     if(f != null) {
  130.         if(f.isDirectory()) {
  131.         return true;
  132.         }
  133.         String extension = getExtension(f);
  134.         if(extension != null && filters.get(getExtension(f)) != null) {
  135.         return true;
  136.         };
  137.     } 
  138.     return false;
  139.     }
  140.  
  141.     /**
  142.      * Return the extension portion of the file's name . 
  143.      *
  144.      * @see #getExtension
  145.      * @see FileFilter#accept
  146.      */
  147.      public String getExtension(File f) {
  148.     if(f != null) {
  149.         String filename = f.getName();
  150.         int i = filename.lastIndexOf('.');
  151.         if(i>0 && i<filename.length()-1) {
  152.         return filename.substring(i+1).toLowerCase();
  153.         };
  154.     } 
  155.     return null;
  156.     }
  157.  
  158.     /**
  159.      * Adds a filetype "dot" extension to filter against. 
  160.      *
  161.      * For example: the following code will create a filter that filters
  162.      * out all files except those that end in ".jpg" and ".tif":
  163.      *
  164.      *   ExampleFileFilter filter = new ExampleFileFilter();
  165.      *   filter.addExtension("jpg");
  166.      *   filter.addExtension("tif");
  167.      *
  168.      * Note that the "." before the extension is not needed and will be ignored.
  169.      */
  170.     public void addExtension(String extension) {
  171.     if(filters == null) {
  172.         filters = new Hashtable(5);
  173.     }
  174.     filters.put(extension.toLowerCase(), this);
  175.     fullDescription = null;
  176.     }
  177.  
  178.  
  179.     /**
  180.      * Returns the human readable description of this filter. For
  181.      * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
  182.      *
  183.      * @see setDescription
  184.      * @see setExtensionListInDescription
  185.      * @see isExtensionListInDescription
  186.      * @see FileFilter#getDescription
  187.      */
  188.     public String getDescription() {
  189.     if(fullDescription == null) {
  190.         if(description == null || isExtensionListInDescription()) {
  191.         if(description != null) {
  192.             fullDescription = description;
  193.         }
  194.         fullDescription += " (";
  195.         // build the description from the extension list
  196.         Enumeration extensions = filters.keys();
  197.         if(extensions != null) {
  198.             fullDescription += "." + (String) extensions.nextElement();
  199.             while (extensions.hasMoreElements()) {
  200.             fullDescription += ", " + (String) extensions.nextElement();
  201.             }
  202.         }
  203.         fullDescription += ")";
  204.         } else {
  205.         fullDescription = description;
  206.         }
  207.     } 
  208.     return fullDescription;
  209.     }
  210.  
  211.     /**
  212.      * Sets the human readable description of this filter. For
  213.      * example: filter.setDescription("Gif and JPG Images");
  214.      *
  215.      * @see setDescription
  216.      * @see setExtensionListInDescription
  217.      * @see isExtensionListInDescription
  218.      */
  219.     public void setDescription(String description) {
  220.     this.description = description;
  221.     fullDescription = null;
  222.     }
  223.  
  224.     /**
  225.      * Determines whether the extension list (.jpg, .gif, etc) should
  226.      * show up in the human readable description.
  227.      *
  228.      * Only relevent if a description was provided in the constructor
  229.      * or using setDescription();
  230.      *
  231.      * @see getDescription
  232.      * @see setDescription
  233.      * @see isExtensionListInDescription
  234.      */
  235.     public void setExtensionListInDescription(boolean b) {
  236.     useExtensionsInDescription = b;
  237.     fullDescription = null;
  238.     }
  239.  
  240.     /**
  241.      * Returns whether the extension list (.jpg, .gif, etc) should
  242.      * show up in the human readable description.
  243.      *
  244.      * Only relevent if a description was provided in the constructor
  245.      * or using setDescription();
  246.      *
  247.      * @see getDescription
  248.      * @see setDescription
  249.      * @see setExtensionListInDescription
  250.      */
  251.     public boolean isExtensionListInDescription() {
  252.     return useExtensionsInDescription;
  253.     }
  254. }
  255.